'''
Created on 2011-10-02

@author: Michalis Famelis
'''

import sys, os

from editor.control import SambaPlayer

try:  
	import pygtk
	pygtk.require("2.0")  
except:  
	pass  
try:  
	import gtk  
except:  
	print("GTK Not Availible")
	sys.exit(1)
	
class gui:
	def __init__(self):
		if os.getcwd().split('/')[-1]=="editor":
			filename= os.getcwd()+"/gui.glade"
		else:
			filename= os.getcwd()+"/editor/gui.glade"
		self.builder = gtk.Builder()
		self.builder.add_from_file(filename)
		self.builder.connect_signals(self)
		
		self.SambaPlayer = SambaPlayer(self)
		
		self.mainWindow =self.builder.get_object("mainWindow")
		self.mainWindow.set_size_request(400, 400)
		self.mainWindow.show()
		
		self.scriptEditor = self.builder.get_object("scriptEditor")
		self.textEntry = self.builder.get_object("textEntry")
		self.status = self.builder.get_object("status")
		
		self.sambaTune =""
		
	def bye(self):
		self.SambaPlayer.finalize()
		gtk.main_quit()

		
	def on_mainWindow_destroy(self, widget):
		self.bye()
		
	def on_playButton_clicked(self, widget):
		self.SambaPlayer.play()
		
	def on_pauseButton_clicked(self, widget):
		self.SambaPlayer.pause()
	
	def on_stopButton_clicked(self, widget):
		self.SambaPlayer.stop()

	def on_menuPlay_activate(self, widget):
		self.SambaPlayer.play()
				
	def on_menuSave_activate(self, widget):
		pass
	
	def on_menuSaveAs_activate(self, widget):
		pass	
	
	def on_menuQuit_activate(self, widget):
		self.bye()
	
	def on_menuAbout_activate(self, widget):
		about = self.builder.get_object("about")
		about.run()
		about.hide()
		
	def on_addCommandButton_clicked(self, widget):
		buf = self.scriptEditor.get_buffer()
		end = buf.get_end_iter()
		command = self.textEntry.get_text()+"\n"
		buf.insert(end, command)
		self.textEntry.set_text("")
		start = buf.get_start_iter()
		self.sambaTune = buf.get_text(start, end, True)
		
	def on_clearEditorButton_clicked(self, widget):
		buf = self.scriptEditor.get_buffer()
		buf.set_text("")
	
	def on_scriptEditor_focus_out_event(self, widget, what):
		buf = self.scriptEditor.get_buffer()
		start = buf.get_start_iter()
		end = buf.get_end_iter()
		self.sambaTune = buf.get_text(start, end, True)
		
	def setStatus(self, status_message):
		self.status.set_text(status_message)
